package com.ge.modules.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ge.common.annotation.LoginUserApi;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbCollectDto;
import com.ge.modules.api.service.TbCollectService;
import com.ge.modules.api.service.TbPostService;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.api.service.TbUserService;
import com.ge.modules.entity.TbCollectEntity;
import com.ge.modules.entity.TbPostEntity;
import com.ge.modules.entity.TbStoreEntity;
import com.ge.modules.entity.TbUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户收藏表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-17 15:12:57
 */
@RestController
@RequestMapping("/api/user/collect")
public class TbCollectController {
    @Autowired
    private TbCollectService collectService;

    @Autowired
    private TbPostService tbPostService;

    @Autowired
    private TbStoreService tbStoreService;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 查询用户收藏列表
     *
     * @param tbUser
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) {
        params.put("userIdMaster", tbUser.getUserId());
        //查询列表数据
        Query query = new Query(params);
        List<TbCollectEntity> collectList = collectService.queryList(query);
        int total = collectService.queryTotal(query);
        List<TbCollectDto> collectDtoList = new ArrayList<>();
        for (int i = 0; i < collectList.size(); i++) {
            TbCollectDto tbCollectDto = collectList.get(i).toDto();
            //判断收藏类型 1店铺 2帖子
            if(tbCollectDto.getCollectType() == 1){
                //店铺id
                Integer storeId = tbCollectDto.getCollectIdSlave();
                TbStoreEntity tbStoreEntity = tbStoreService.queryByStoreId(storeId);
                tbCollectDto.setStoreName(tbStoreEntity.getStoreName());
                tbCollectDto.setStoreHeadImg(tbStoreEntity.getHeadImg());
            }else if(tbCollectDto.getCollectType() == 2){
                //帖子id
                Integer postId = tbCollectDto.getCollectIdSlave();
                TbPostEntity tbPostEntity = tbPostService.queryObject(postId);
                TbUser user = tbUserService.queryById(tbPostEntity.getUserId());
                tbCollectDto.setPostDescribe(tbPostEntity.getPostDescribe());
                tbCollectDto.setUserName(user.getNickname());
                tbCollectDto.setHeadImg(user.getHeadPicture());
            }
            collectDtoList.add(tbCollectDto);
        }
        PageUtils pageUtil = new PageUtils(collectDtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 添加收藏 or 取消收藏
     *
     * @param tbUser
     * @param collect
     * @return
     */
    @PostMapping("/save")
    public R save(@LoginUserApi TbUser tbUser, TbCollectEntity collect) {
        //判断是否收藏过
        collect.setUserIdMaster(tbUser.getUserId());
        TbCollectEntity collectEntity = collectService.queryByObject(collect);
        //收藏类型 0 未收藏 1 已收藏
        int collectType = 0;
        if(collectEntity != null){
            collectService.delete(collectEntity.getId());
        }else{
            collect.setCreateTime(new Date());
            collectService.save(collect);
            collectType = 1;
        }
        return R.ok().put("collectType",collectType);
    }

    /**
     * 删除收藏记录
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public R delete(Integer[] ids) {
        collectService.deleteBatch(ids);
        return R.ok();
    }

}
